package allenja.cp2011.MVC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbConnectionFactory {
	String derbyDriverClassName = "org.apache.derby.jdbc.EmbeddedDriver";
	String pathToLocalTempDirectory = System.getProperty("java.io.tmpdir");
	String connectionUrl = "jdbc:derby:" + pathToLocalTempDirectory + "ChatServerDB;create=true";
	
	private static DbConnectionFactory connectionFactory = null;
	
	// Default constructor
	private DbConnectionFactory() {
		try {
			Class.forName(derbyDriverClassName);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	// Create the connection to the database
	public Connection getConnection() throws SQLException {
		Connection conn = null;
		conn = DriverManager.getConnection(connectionUrl);
		return conn;
	}
	
	// Create an instance of the database connection
	public static DbConnectionFactory getInstance() {
		if (connectionFactory == null) {
			connectionFactory = new DbConnectionFactory();
		}
		return connectionFactory;
	}
}
